/* eslint-disable */
import UedService from '../../service/ude'
export default {
  data() {
    return {
      isLoadingBtn: false,
      historyItems: [],
      items: [],
      name: '',
      menuItems: []
    }
  },

  watch: {
    $route(to, from) {
      console.log(this.$route.params.path)
      if (this.$route.params.path) {
        this.menuItems = this.$route.params.path.split('/').filter(item => item != '')
      }
      this.getFiles()
    }
  },

  mounted() {
    if (this.$route.params.path) {
      this.menuItems = this.$route.params.path.split('/').filter(item => item != '')
    }
    this.getFiles()

    let uedBackHistoryItems = localStorage.getItem('uedBackHistoryItems')
    if (uedBackHistoryItems) {
      this.historyItems = JSON.parse(uedBackHistoryItems)
    }
  },
  methods: {

    // 搜索按钮
    search(name) {
      if (name) {
        this.name = name
      }
      if (this.name != '') {
        let index = this.historyItems.indexOf(this.name)
        if (index > -1) {
          this.historyItems.splice(index, 1)
          this.historyItems.unshift(this.name)
        } else {
          this.historyItems.unshift(this.name)
          if (this.historyItems.length > 5) {
            this.historyItems.splice(this.historyItems.length - 1, 1)
          }
        }
        localStorage.setItem('uedBackHistoryItems', JSON.stringify(this.historyItems))
      }
      this.getFiles()
    },

    getFiles(item) {
      let param = {
        name: this.name,
        fullPath: this.$route.params.path || ''
      }
      if (item) {
        param.fullPath = item.fullPath
      }
      this.isLoadingBtn = true
      UedService.getFiles(param).then(res => {
        if (res.success) {
          let hasIndexHtml = false;
          if (res.data && res.data.length > 0) {
            res.data.forEach(file => {
              if (file.fileName == 'index.html' || file.fileName == 'index.htm') {
                hasIndexHtml = true
              }
            })
          }
          if (!hasIndexHtml) {
            if (item) {
              this.$router.push('/index/' + encodeURIComponent(item.fullPath))
            }
            this.items = res.data;
          } else {
            window.open(item.fullPath, '_blank');
          }
        }
      }).finally(() => {
        this.isLoadingBtn = false
      })
    },

    downLoad(item) {
      window.open(item.fullPath)
    },

    rowClick(item) {
      if (item.isDirectory) {
        this.name = ''
        this.getFiles(item)
      } else if (!item.isDownLoad) {
        window.open(item.fullPath)
      }
    },

    clickMenuItem(item, index) {
      this.name = ''
      if (item) {
        this.menuItems = this.menuItems.filter((item, idx) => idx <= index)
        let path = this.menuItems.join('/')
        console.log(path)
        this.$router.push('/index/' + encodeURIComponent(path))
      } else {
        this.menuItems = []
        this.$router.push('/index')
      }
    }
  },
  filters: {
    formatDate(m, fmt) {
      let date = new Date(m)
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + ''
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (('00' + str).substr(str.length)))
        }
      }
      return fmt
    }
  }
}